using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ISB_iCard
{
    public partial class iCardLogin : Form
    {
        public iCardLogin()
        {
            InitializeComponent();
        }
        iCardService.Service1 objiCardService = new ISB_iCard.iCardService.Service1();
        public string userid = string.Empty;
        iCardMain objMain;
        private void iCardLogin_Load(object sender, EventArgs e)
        {
            statusBar1.Text = "Ready";
            this.KeyDown += new KeyEventHandler(this.OnKeyDown);
            objMain = new iCardMain();
        }

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    btnLogin_Click(sender, e);
                    break;

                case Keys.Escape:
                    btnClear_Click(sender, e);
                    break;

                //default:
                //    MessageBox.Show(e.KeyCode.ToString() + " pressed.", "Key Down Event");
                //    break;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (btnLogin.Enabled)
            {
                btnLogin.Enabled = false;
                statusBar1.Text = "Logging in";
                statusBar1.Refresh();
                objiCardService.Url = "http://172.18.8.64:81/ISB_Data/Service1.asmx";
                userid = objiCardService.GetLogin(txtUserName.Text, txtPassword.Text);
                if (userid != string.Empty)
                {
                    MessageBox.Show("Authenticated");
                    statusBar1.Text = "Authenticated";
                    statusBar1.Refresh();
                    objMain.userid = userid;
                    objMain.Show();
                }
                else
                {
                    MessageBox.Show("Invalid User Name or Password");
                    statusBar1.Text = "Invalid User Name or Password";
                    btnClear_Click(sender, e);
                }
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtUserName.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtUserName.Focus();
            btnLogin.Enabled = true;
        }

        private void txtUserName_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    btnLogin_Click(sender, e);
                    break;

                case Keys.Escape:
                    btnClear_Click(sender, e);
                    break;

                //default:
                //    MessageBox.Show(e.KeyCode.ToString() + " pressed.", "Key Down Event");
                //    break;
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    btnLogin_Click(sender, e);
                    break;

                case Keys.Escape:
                    btnClear_Click(sender, e);
                    break;

                //default:
                //    MessageBox.Show(e.KeyCode.ToString() + " pressed.", "Key Down Event");
                //    break;
            }
        }
    }
}